<?php
    class admin_productManage_Controller extends controllerBase
    {
        public function process()
        {
            $iLimit = 30;
            $this->aParams['limit'] = $iLimit;
            $iPage = isset($_GET['page']) ? $_GET['page'] : 1;
            $this->aParams['page'] = $iPage;
            database::isUser(true);
            $oDatabase = new database();
            $sQuery = "
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX bus: <".URL::getLink('id/bussiness#').">

            SELECT COUNT(*)  as ?total
            WHERE 
            {
            ?uri rdf:type bus:product.
            }";
            $result = @$oDatabase->loadQuery($sQuery);
            $iTotal = (int)$result['result']['rows'][0]['total'];
            $this->aParams['total'] = $iTotal;
            
            $sQuery = "
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX bus: <".URL::getLink('id/bussiness#').">

            SELECT DISTINCT *  
            WHERE 
            {
            ?uri rdf:type bus:product.
            ?uri bus:name ?name.
            ?uri bus:ownUser ?userUri.
            ?userUri foaf:familyName ?userName.
            ?uri bus:publishTime ?time.
            ?uri bus:description ?description.
            ?uri bus:hasCategory ?categoryUri.
            ?categoryUri bus:name ?categoryName.
            ?uri bus:defaultImage ?defaultImage.
            ".(!database::isAdmin() ? "FILTER(?userUri = '".$_SESSION['user_uri']."')." : '')."
            }
            ORDER BY DESC(?time)
            LIMIT ".$iLimit."  
            OFFSET ".(($iPage - 1) * $iLimit)." 
            ";

            $result = $oDatabase->loadQuery($sQuery);
            $this->aParams['aProducts'] = $result['result']['rows'];
            
            // Get number page to show
            $iNumberPages = $iTotal / $iLimit;
            $this->aParams['numberPage'] = $iNumberPages;
        }
    }
?>
